import java.util.Comparator;
import java.util.PriorityQueue;

public class Solution1801 {
    public int getNumberOfBacklogOrders(int[][] orders) {
        PriorityQueue<int[]> q1=new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
        PriorityQueue<int[]> q2=new PriorityQueue<>((a,b)->b[0]-a[0]);
        for (int[] order : orders) {
            int t=order[1];
            if (order[2]==0){
                while (!q1.isEmpty() && q1.peek()[0]<=order[0] && t>0){
                    int p=q1.peek()[1];
                    if (p<=t){
                        t-=p;
                        q1.poll();
                    }else {
                        q1.peek()[1]-=t;
                        t=0;
                    }
                }
                if (t>0){
                    q2.offer(new int[]{order[0],t});
                }
            }else {
                while (!q2.isEmpty() && q2.peek()[0]>=order[0] && t>0){
                    int p=q2.peek()[1];
                    if (p<=t){
                        t-=p;
                        q2.poll();
                    }else {
                        q2.peek()[1]-=t;
                        t=0;
                    }
                }
                if (t>0){
                    q1.offer(new int[]{order[0],t});
                }
            }
        }
        long ans=0;
        for (int[] ints : q1) {
            ans=(ans+ints[1])%1000000007;
        }
        for (int[] ints : q2) {
            ans=(ans+ints[1])%1000000007;
        }
        return (int)ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1801().getNumberOfBacklogOrders(new int[][]{{10,5,0},{15,2,1},{25,1,1},{30,4,0}}));
    }
}
